<?php
// No direct access to this file
defined('_JEXEC') or die('Restricted access');
JHtml::_('behavior.tooltip');
?>
<form  method="post" name="adminForm">
    <div id="lop">
        <h2>Danh sách lớp</h2><br>
        <div class="lop-left">
            Học kì:  <select name="ma_hocki" id="ma_hocki">
                <?php
                foreach ($this->items as $i => $item):
                    echo "<option value='$item->ma_hocki'>$item->ma_hocki</option>";
                endforeach;
                ?>
            </select>  
        </div>
        <div class="lop-right">
            Tên học phần:<input name="ten_hp" type="text" id="ten_hp" value="<?php echo $_POST["ten_hp"] ?>" size="10">
        </div>
        <div class="lop-left">
            Mã lớp : <input name="ma_lop" type="text" id="ma_lop" value="<?php echo $_POST["ma_lop"] ?>" size="10">
        </div>
        <div class="lop-right">
            Tên giảng viên:<input name="ten_gv" type="text" id="ten_gv" value="<?php echo $_POST["ten_gv"] ?>" size="10">
        </div>
        <div class="clear"></div>
        <div class="lop-middle">
            <input type="submit" value="Tìm kiếm">
        </div>
        <?php
        $ma_lop = $_POST["ma_lop"];
        $ten_hp = $_POST["ten_hp"];
        $ten_gv = $_POST["ten_gv"];
        $ma_hocki = $_POST["ma_hocki"];
        ?>
        <table id="sinhvienList" class="table table-striped">
            <thead>
                <tr>
                    <th class="left">
                        Mã học phần
                    </th>
                    <th class="left">
                        Tên học phần
                    </th>
                    <th class="left">
                        Tên giảng viên
                    </th>
                    <th class="left">
                        Mã lớp
                    </th>
                    <th class="left">
                        Tổng SV
                    </th>
                    <th class="left">
                        Học kì
                    </th>

                    <th class="left">
                        Detail
                    </th>
                </tr>
            </thead>
            <tbody>
                 <?php
                $db = JFactory::getDbo();
                $query = $db->getQuery(true);
                $query->select('*');
                $query->from('#__lop');
                $query->where('ma_lop like "%' . $ma_lop . '%" AND ten_hp like "%' . $ten_hp . '%" AND ten_gv like "%' . $ten_gv . '%" AND ma_hocki like "%' . $ma_hocki . '%"');
                 $query->order('ma_hocki DESC');
                $db->setQuery($query);
                $rows = $db->loadObjectList();
               foreach ($rows AS $key => $item) {
                    ?>
                        <tr>
                            <th>
                                <?php echo $item->ma_hp; ?>
                            </th>
                            <th>
                                <?php echo $item->ten_hp; ?>
                            </th>
                            <th>
                                <?php echo $item->ten_gv; ?>
                            </th>
                            <th>
                                <?php echo $item->ma_lop; ?>
                            </th>
                            <th>
                                <?php
                                $db = JFactory::getDbo();
                                $query = $db->getQuery(true);
                                $query->select('count(a.ma_sv) AS tong');
                                $query->from('#__dangkilop AS a');
                                $query->join('LEFT', '#__lop AS b ON b.ma_lop = a.ma_lop');
                                $query->where('a.ma_lop=' . $item->ma_lop);
                                $db->setQuery($query);
                                $rows = $db->loadObjectList();
                                foreach ($rows AS $key => $list) {
                                    echo "$list->tong";
                                }
                                ?>  
                            </th>
                            <th>
                                <?php echo $item->ma_hocki; ?>
                            </th>
                            <th align = "center">
                      <a href="<?php echo JRoute::_('index.php?option=com_sinhvien&view=lop&id=' . (int) $item->id); ?>">Chi tiết</a>
                    </th>
                    </tr>

                <?php
                }
            ?>
            </tbody>

        </table>
    </div>
</form>